<template>
  <bpm-c-subtitle :title="getLanguage('title.postingInfo')"
                  :isCollapse="false">
    <bpm-c-form>
    <bpm-c-block :preadonly="readonlys[0]"
                 :ptitle="titles[0]"
                 :pinputtype="inputTypes[0]"
                 v-show="showBlocks[0]"
                 v-model="companycode"/>
    <bpm-c-block :preadonly="readonlys[1]"
                 :ptitle="titles[1]"
                 :pinputtype="inputTypes[1]"
                 v-show="showBlocks[1]"
                 v-model="currency"/>
    </bpm-c-form>
    <bpm-c-form>
    <bpm-c-block :preadonly="readonlys[2]"
                 :ptitle="titles[2]"
                 :pinputtype="inputTypes[2]"
                 v-show="showBlocks[2]"
                 v-model="certificatedate"/>
    <bpm-c-block :preadonly="readonlys[3]"
                 :ptitle="titles[3]"
                 :pinputtype="inputTypes[3]"
                 v-show="showBlocks[3]"
                 v-model="postingdate"/>
    </bpm-c-form>
    <bpm-c-form>
    <bpm-c-block :preadonly="readonlys[4]"
                 :ptitle="titles[4]"
                 :pinputtype="inputTypes[4]"
                 v-show="showBlocks[4]"
                 v-model="totalSumFee"/>
    <bpm-c-block :preadonly="readonlys[5]"
                 :ptitle="titles[5]"
                 :pinputtype="inputTypes[5]"
                 v-show="showBlocks[5]"
                 v-model="defraySum"/>
    </bpm-c-form>
    <bpm-c-form>
    <bpm-c-block :preadonly="readonlys[6]"
                 :ptitle="titles[6]"
                 :pinputtype="inputTypes[6]"
                 v-show="showBlocks[6]"
                 v-model="certificateNo"/>
    <bpm-c-block :preadonly="readonlys[7]"
                 :ptitle="titles[7]"
                 :pinputtype="inputTypes[7]"
                 v-show="showBlocks[7]"
                 @buttonClick="buttonClick"
                 v-model="totalsum"/>
    </bpm-c-form>
    <bpm-d-grid v-loading="showLoading"
                v-show="showGrid"
                :showCheckbox="true"
                head="post"
                ref="postallgrid"
                :gridInfo="ssc.reimburse.whBtReimburseRcmDetail"
                :gridTitleInfo="gridTitleInfo"
                :gridShowInfo="gridShowInfo"
                :gridEditInfo="gridEditInfo"
                :gridWidthInfo="gridWidthInfo"
                :gridTypeInfo="gridTypeInfo"
                :pageMax="pageMax"
                :pageStart="pageStart"
                :pageCount="pageCount"
                :validateExp="validateExp"
                @gridStatusChanged="gridStatusChanged"
                @gridPageChanged="gridPageChanged"
                @gridClicked="gridClicked"
                @gridDBLClicked="gridDBLClicked"
                @addClicked="addClicked"
                @deleteClicked="deleteClicked"
                @makeCertClicked="makeCertClicked"
                @gridCheckedChanged="gridCheckedChanged"
                @saveClicked="saveClicked"
                @gridCellEditEnd="gridCellEditEnd"
    />
  </bpm-c-subtitle>
</template>

<script>
import {mapState, mapActions} from 'vuex'

export default {
  name: 'bpmDPostarea',
  data () {
    let gridShowInfo = ['costcentername', 'orderName', 'accoutingsubject', 'subjectcode', 'notincludetaxessumBx', 'taxrateBx', 'transferTaxBx']
    // 保证总和为980，如果有复选框则总和为940
    let gridWidthInfo = [150, 150, 140, 125, 125, 125, 125]
    return {
      validateExp: {
        3: {
          flag: true,
          maxlength: 14,
          require: true,
          positiveDecimal: true
        },
        4: {
          flag: true,
          maxlength: 14,
          require: true,
          positiveDecimal: true
        },
        5: {
          flag: true,
          maxlength: 14,
          positiveDecimal: true
        }
      },
      gridEditItem: '',
      gridSelectIndex: '',
      gridShowInfo: gridShowInfo,
      gridWidthInfo: gridWidthInfo,
      inputTypes: ['text', 'sltBB', 'date', 'date', 'number', 'number', 'text', 'button'],
      gridTypeInfo: ['text', 'text', 'text', 'text', 'number', 'number', 'number'],
      readonlys: [true, true, true, false, true, true, true, true],
      showBlocks: [true, true, true, true, true, true, true, true],
      shows: [true, true, true, true, true, true, true, true],
      shows2: [true, true, true, true, true, true],
      showGrid: true,
      gridLoading: false
    }
  },
  computed: {
    ...mapState(['pageTypeEnum', 'pageType', 'tsPiid', 'ssc', 'gridSelectRow', 'gridPageInfo', 'lang', 'dialogloading']),
    showLoading () {
      return this.gridLoading || (this.dialogloading !== 0)
    },
    gridTitleInfo () {
      return [
        this.getLanguage('label.whBtReimburseRcmDetails.costcentername'),
        this.getLanguage('label.whBtReimburseRcmDetails.order'),
        this.getLanguage('label.whBtReimburseRcmDetails.accoutingsubject'),
        this.getLanguage('label.whVbtReimburseRcmDetails.subjectcode'),
        this.getLanguage('label.whBtReimburseRcmDetails.notincludetaxessumBx'),
        this.getLanguage('label.whBtReimburseRcmDetails.taxrateBx'),
        this.getLanguage('label.whBtReimburseRcmDetails.transferTaxBx')
      ]
    },
    gridEditInfo () {
      let gridEditInfo = []
      if (this.pageType === this.pageTypeEnum.approveFee) {
        gridEditInfo = ['subjectcode', 'transferTaxBx']
      }
      return gridEditInfo
    },
    titles () {
      return [
        this.getLanguage('label.whBtReimburseRcmSub.companycode'),
        this.getLanguage('label.whBtReimburseRcmSub.currency'),
        this.getLanguage('label.whBtReimburseRcmSub.certificatedate'),
        this.getLanguage('label.whBtReimburseRcmSub.postingdate'),
        this.getLanguage('label.whBtReimburseRcmSub.totalSumFee'),
        this.getLanguage('label.whBtReimburseRcmSub.defraySum'),
        this.getLanguage('label.whBtReimburseRcmSub.certificateNo'),
        this.getLanguage('title.button.rejectImage')
      ]
    },
    pageMax () {
      return this.gridPageInfo.ssc.reimburse.whBtReimburseRcmDetail.max
    },
    pageStart () {
      return this.gridPageInfo.ssc.reimburse.whBtReimburseRcmDetail.start
    },
    pageCount () {
      return this.gridPageInfo.ssc.reimburse.whBtReimburseRcmDetail.count
    },
    companycode: {
      get () {
        return this.ssc.reimburse.whBtReimburseRcmSub.companycode
      },
      set (value) {
        // do nothing.
      }
    },
    currency: {
      get () {
        return this.ssc.reimburse.whBtReimburseRcmSub.currency
      },
      set (value) {
        // do nothing.
      }
    },
    certificatedate: {
      get () {
        return this.ssc.reimburse.whBtReimburseRcmSub.certificatedate
      },
      set (value) {
        // do nothing.
      }
    },
    postingdate: {
      get () {
        return this.ssc.reimburse.whBtReimburseRcmSub.postingdate
      },
      set (value) {
        this.$store.commit('updateDataSscReimburseWhBtReimburseRcmSub', {
          name: 'postingdate', value: value
        })
      }
    },
    totalSumFee: {
      get () {
        return this.ssc.reimburse.whBtReimburseRcmSub.totalSumFee
      },
      set (value) {
        this.$store.commit('updateDataSscReimburseWhBtReimburseRcmSub', {
          name: 'totalSumFee', value: value
        })
      }
    },
    defraySum: {
      get () {
        return this.ssc.reimburse.whBtReimburseRcmSub.defraySum
      },
      set (value) {
        this.$store.commit('updateDataSscReimburseWhBtReimburseRcmSub', {
          name: 'defraySum', value: value
        })
      }
    },
    certificateNo: {
      get () {
        return this.ssc.reimburse.whBtReimburseRcmSub.certificateNo
      },
      set (value) {
        this.$store.commit('updateDataSscReimburseWhBtReimburseRcmSub', {
          name: 'certificateNo', value: value
        })
      }
    },
    totalsum: {
      get () {
        return this.ssc.reimburse.whBtReimburseRcmSub.totalsum
      },
      set (value) {
        this.$store.commit('updateDataSscReimburseWhBtReimburseRcmSub', {
          name: 'totalsum', value: value
        })
      }
    }
  },
  methods: {
    ...mapActions(['saveTableWh_bt_reimburse_rcm_details', 'getDataFromAJAX_Reim_005_ApproveFee_Open',
      'getDataFromTableRcmDetails', 'rejectPictureLink', 'queryBudgetSubject', 'approveFeeCallAjaxCheckBeforeMaking',
      'makeCert', 'saveDataBeforeMakeCert']),
    // TODO 驳回影像按钮点击事件
    buttonClick () {
      console.log('&&&&&&&&&&&&&&')
      let certificateNo = this.$store.state.ssc.reimburse.whBtReimburseRcmSub.certificateNo
      let processlinkopentext = this.$store.state.ssc.reimburse.whBtReimburse.processlinkopentext
      if (certificateNo != null && certificateNo !== '') {
        this.alert(this.getResource('alert.yzzbyxbhyx')) // 已经制证,不允许驳回影像
        return
      }
      if (processlinkopentext == null || processlinkopentext === '') {
        this.alert(this.getResource('alert.noPicture')) // 无影像可驳回 alert.noPicture
        return
      }
      this.rejectPictureLink({me: this})
      this.alert(this.getResource(this.$store.state.retMsg))
    },
    selectClick () {
      console.log('sssssss', this.ssc)
    },
    makeCertClicked () {
      // 1:前端数据检查
      if (this.isMakeCert() === '-1') {
        this.alert(this.getResource('alert.bncfzz')) // 已经制证,不允许再次制证
        return false
      }
      let postDate = this.$store.state.ssc.reimburse.whBtReimburseRcmSub.postingdate
      if (postDate === null || postDate === '') {
        this.alert(this.getResource('alert.information.gzrqbnwk')) // 过账日期不能为空
        return
      }
      // 2、调用ajax后端进行数据检查
      this.approveFeeCallAjaxCheckBeforeMaking({
        me: this,
        data: 'approveFee'
      })
      if (this.$store.state.retMsg !== 'pass') {
        this.alert(this.getResource(this.$store.state.retMsg))
        this.$store.state.retMsg = ''
        return
      }
      // 3、校验通过，保存数据，/制证前保存已计算冲减数据等；
      let date = this.$store.state.ssc.reimburse.whBtReimburseRcmSub.postingdate
      console.log('date====', date)
      let postingDateStr = this.convertDate(date)
      console.log('postingDateStr=======', postingDateStr)
      this.saveDataBeforeMakeCert({
        me: this,
        postingDate: postingDateStr,
        defraySum: this.$store.state.ssc.reimburse.whBtReimburseRcmSub.defraySum,
        totalSumFee: this.$store.state.ssc.reimburse.whBtReimburseRcmSub.totalSumFee,
        chargeAgainstSumFee: this.$store.state.ssc.reimburse.whBtReimburseRcmSub.chargeAgainstSumFee,
        unpaidAggregateAmountFee: this.$store.state.ssc.reimburse.whBtReimburseRcmSub.unpaidAggregateAmountFee
      })
      if (this.$store.state.retMsg !== 'pass') {
        this.alert(this.getResource(this.$store.state.retMsg)) // 更新制证数据时异常
        this.$store.state.retMsg = ''
        return
      }
      // 4、通过AJAX调用接口进行制证操作
      let againstsum = this.$store.state.ssc.reimburse.whBtReimburseRcmSub.chargeAgainstSumFee
      if (againstsum == null || againstsum === '') {
        againstsum = '0'
      }
      this.makeCert({
        me: this,
        'againstsum': againstsum
      })
      if (this.$store.state.retMsg !== 'pass') {
        this.alert(this.getResource(this.$store.state.retMsg)) // 制证时出现异常
        this.$store.state.retMsg = ''
      } else {
        this.$store.state.ssc.reimburse.whBtReimburseRcmSub.certificateNo = this.$store.state.certNo
      }
    },
    selectClick2 () {
    },
    gridClicked (index) {
      this.gridSelectIndex = index
      this.$store.state.gridSelectIndex.whBtReimburseRcmDetail = index
    },
    gridDBLClicked (index, item) {
      this.gridEditItem = item
      this.$store.state.gridSelectRow.ssc.reimburse.whBtReimburseRcmDetail = item
      console.log('制证list的form首次取值gridEditItem', this.$store.state.gridSelectRow.ssc.reimburse.whBtReimburseRcmDetail)
      if (item && item.subjectcode) {
        this.gridLoading = true
        setTimeout(() => {
          let param = {}
          param.me = this
          param.companyCode = this.$store.state.ssc.reimburse.whBtReimburse.reimbursecompanycode
          param.costingCenterCode = item.costcentercode
          param.inwardOrderCode = item.ordercode
          param.budgetSubjectName = ''
          param.budgetSubjectCode = ''
          param.flag = 'post'
          this.queryBudgetSubject(param)
          this.gridLoading = false
        }, 100)
      }
    },
    addClicked () {
      // 费用岗增加行项目
      console.log('add post', this.$store.state.gridSelectRow.ssc.reimburse.whBtReimburseCostshare)
      this.$store.state.gridSelectRow.ssc.reimburse.whBtReimburseRcmDetail.mid = this.$store.state.gridSelectRow.ssc.reimburse.whBtReimburseCostshare.mid
      this.$store.state.gridSelectRow.ssc.reimburse.whBtReimburseRcmDetail.costcentercode = this.$store.state.gridSelectRow.ssc.reimburse.whBtReimburseCostshare.costcentercode
      this.$store.state.gridSelectRow.ssc.reimburse.whBtReimburseRcmDetail.costcentername = this.$store.state.gridSelectRow.ssc.reimburse.whBtReimburseCostshare.costcentername
      this.$store.state.gridSelectRow.ssc.reimburse.whBtReimburseRcmDetail.subjectcode = this.$store.state.gridSelectRow.ssc.reimburse.whBtReimburseCostshare.subjectcode
      this.$store.state.gridSelectRow.ssc.reimburse.whBtReimburseRcmDetail.accoutingsubject = this.$store.state.gridSelectRow.ssc.reimburse.whBtReimburseCostshare.subjectname
      this.$store.state.gridSelectRow.ssc.reimburse.whBtReimburseRcmDetail.ordercode = this.$store.state.gridSelectRow.ssc.reimburse.whBtReimburseCostshare.internalordercode
      this.$store.state.gridSelectRow.ssc.reimburse.whBtReimburseRcmDetail.orderName = this.$store.state.gridSelectRow.ssc.reimburse.whBtReimburseCostshare.internalordername
      this.$store.state.gridSelectRow.ssc.reimburse.whBtReimburseRcmDetail.tsId = this.$store.state.tsId
      this.$store.state.gridSelectRow.ssc.reimburse.whBtReimburseRcmDetail.mmid = 'MMID-' + this.getUuid()
      let param = {}
      param.me = this
      param.companyCode = this.$store.state.ssc.reimburse.whBtReimburse.reimbursecompanycode
      param.costingCenterCode = this.$store.state.ssc.reimburse.whBtReimburse.reimbursecostcentercode
      param.inwardOrderCode = this.$store.state.gridSelectRow.ssc.reimburse.whBtReimburseRcmDetail.ordercode
      param.budgetSubjectName = ''
      param.budgetSubjectCode = ''
      param.flag = 'post'
      console.log('inner', param)
      this.queryBudgetSubject(param)
    },
    gridCheckedChanged (obj) {
      let strs = ''
      for (var i = 0; i < obj.length; i++) {
        strs += this.$store.state.ssc.reimburse.whBtReimburseRcmDetail[obj[i]].mmid + ','
      }
      if (strs !== '') {
        strs = strs.substring(0, strs.length - 1)
      }
      this.$store.state.fygbxmcCheckedMmids = strs
    },
    saveClicked () {
      console.log('保存gridEditItem----', this.gridEditItem)
      console.log('保存数据中心gridEditItem----', this.$store.state.gridSelectRow.ssc.reimburse.whBtReimburseRcmDetail)
      if (this.isMakeCert() === '-1') {
        this.alert(this.getResource('alert.information.yjzzbyxxg')) // 已经制证不允许修改
        return false
      }
      let noTaxMoney = this.$store.state.gridSelectRow.ssc.reimburse.whBtReimburseRcmDetail.notincludetaxessum
      let taxMoney = this.$store.state.gridSelectRow.ssc.reimburse.whBtReimburseRcmDetail.taxrate
      let transferTaxBx = this.$store.state.gridSelectRow.ssc.reimburse.whBtReimburseRcmDetail.transferTaxBx
      let notincludetaxessumBx = this.$store.state.gridSelectRow.ssc.reimburse.whBtReimburseRcmDetail.notincludetaxessumBx
      let taxrateBx = this.$store.state.gridSelectRow.ssc.reimburse.whBtReimburseRcmDetail.taxrateBx

      if (noTaxMoney == null || noTaxMoney === 0 || noTaxMoney === '' || isNaN(noTaxMoney) ) {
        this.$store.state.gridSelectRow.ssc.reimburse.whBtReimburseRcmDetail.notincludetaxessum = 0 + ''
      }
      if (notincludetaxessumBx == null || notincludetaxessumBx === 0 || notincludetaxessumBx === '' || isNaN(notincludetaxessumBx)) {
        this.$store.state.gridSelectRow.ssc.reimburse.whBtReimburseRcmDetail.notincludetaxessumBx = 0 + ''
      }
      if (taxMoney == null || taxMoney === 0 || taxMoney === '' || isNaN(taxMoney)) {
        this.$store.state.gridSelectRow.ssc.reimburse.whBtReimburseRcmDetail.taxrate = 0 + ''
      }
      if (transferTaxBx == null || transferTaxBx === 0 || transferTaxBx === '' || isNaN(transferTaxBx)) {
        this.$store.state.gridSelectRow.ssc.reimburse.whBtReimburseRcmDetail.transferTaxBx = 0 + ''
      }
      if (taxrateBx == null || taxrateBx === 0 || taxrateBx === '' || isNaN(taxrateBx)) {
        this.$store.state.gridSelectRow.ssc.reimburse.whBtReimburseRcmDetail.taxrateBx = 0 + ''
      }

      if (noTaxMoney === null) {
        this.alert(this.getResource('alert.information.bhsjebnwkbnwl')) // 不含税金额不能为空
        return false
      }
      if (taxMoney === null) {
        this.alert(this.getResource('alert.information.sebnwkbnwl')) // 税额不能为空
        return false
      }
      // if (taxMoney * 1 > noTaxMoney * 0.165) {
      //   this.alert(this.getResource('alert.information.secghsje')) // 税额不能大于不含税金额的17%
      //   return false
      // }
      console.log('**********', this.$store.state.gridSelectRow.ssc.reimburse.whBtReimburseRcmDetail)
      this.saveTableWh_bt_reimburse_rcm_details({
        me: this,
        data: this.$store.state.gridSelectRow.ssc.reimburse.whBtReimburseRcmDetail
      })
      this.$refs.postallgrid.hideGridHead(true)
      return true
    },
    deleteClicked () {
      if (this.isMakeCert() === '-1') {
        this.alert(this.getResource('alert.information.yjzzwfsc'))
        return
      }
      let mmids = {}
      mmids = this.$store.state.fygbxmcCheckedMmids // 费用岗已选中制证行；
      if (typeof mmids === 'undefined' || mmids === '' || mmids.length === 0) {
        this.alert(this.getResource('alert.qxzhjl'))
        return
      }
      this.httpBpmDelBz({delKey: 'mmid', mids: this.$store.state.fygbxmcCheckedMmids.split(','), tableName: 'wh_vbt_reimburse_rcm_details'})
      this.getDataFromTableRcmDetails({me: this, async: true})
    },
    gridStatusChanged (v) {
      // do nothing
    },
    gridPageChanged (v) {
      console.log('post', v)
      if (v.name === 'count' || v.name === 'start') {
        let json = {
          path: 'whBtReimburseRcmDetail',
          name: v.name,
          value: v.value
        }
        this.$store.commit('upGridPageInfoWithPathAndValue', json)
        this.getDataFromTableRcmDetails({me: this, async: true})
      }
    },
    gridCellEditEnd: function (record, recordFieldValue) {
      this.gridLoading = true
      setTimeout(() => {
        this.$store.state.gridSelectRow.ssc.reimburse.whBtReimburseRcmDetail = record
        if (!this.saveClicked()) {
          this.getDataFromTableRcmDetails({me: this, async: true})
        } else {
          this.tips(this.getResource('alert.tips.sjxgcg'))
        }
        this.gridLoading = false
      }, 100)
    }
  },
  created () {
    // if (this.pageTypeEnum.approveFee === this.pageType) {
    //   this.getDataFromAJAX_Reim_005_ApproveFee_Open({me: this, async: true})
    // }
    // this.getDataFromTableRcmDetails({me: this, async: true})
    if (this.pageTypeEnum.view === this.pageType) {
      this.readonlys = [true, true, true, true, true, true, true, true]
      this.showBlocks = [true, true, true, true, false, true, true, false]
      this.showGrid = false
    }
  }
}
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>
</style>
